package com.mike.bdb.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.ui.support.AbstractBackGroundWorkDialog;
import com.mike.bdb.util.BackThread;
import com.mike.bdb.util.ImageManager;
import com.mike.bdb.util.BackThread.Type;
import com.mike.swt.util.ShellUtil;

public class ExportDialog extends AbstractBackGroundWorkDialog {

	private static final Logger LOG = LoggerFactory.getLogger(ExportDialog.class);
	private Shell motherland;

	private volatile boolean open = false;
	private Display display;
	
	public ExportDialog(Shell motherland) {
		this.motherland = motherland;
	}
	
	public boolean isOpen() {
		return open;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ExportDialog window = new ExportDialog(null);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		createContents();
		shell.open();
		open = true;
		shell.layout();
		
		startUpdateThread();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		open = false;
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setImage(ImageManager.getImage(ImageManager.LOGO_MING));
		shell.setSize(480, 114);
		shell.setText(this.getClass().getSimpleName()); //$NON-NLS-1$
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			{
				{
					Composite composite = new Composite(shell, SWT.NONE);
					composite.setLayout(new GridLayout(3, false));
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					{
						progressBar = new ProgressBar(composite, SWT.SMOOTH);
						{
							GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
							gridData.widthHint = 400;
							progressBar.setLayoutData(gridData);
						}
						progressBar.setSelection(0);
					}
					info = new Label(composite, SWT.NONE);
					info.setText(" 请稍后...");
				}
			}
		}
		if (null != motherland) {
			ShellUtil.setCenter(shell, motherland);
		}
	}
	
	@Override
	protected void startBackWorkThread() {
		backThread = new BackThread(Type.Export);
		backThread.start();
	}
}
